package com.householdregister.people.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import com.householdregister.common.utils.DateUtils;
import com.householdregister.common.utils.SecurityUtils;
import com.householdregister.people.domain.CategroyCount;
import com.householdregister.people.domain.EducationLevel;
import com.householdregister.people.domain.People;
import com.householdregister.people.mapper.PeopleMapper;
import com.householdregister.people.service.IPeopleService;
import com.householdregister.residence.domain.HouseholdRegisterHistory;
import com.householdregister.residence.mapper.HouseholdRegisterHistoryMapper;
import com.householdregister.residence.mapper.HouseholdResidenceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 人员管理Service业务层处理
 * 
 * @author zyj
 * @date 2022-02-22
 */
@Service
public class PeopleServiceImpl implements IPeopleService
{
    @Autowired
    private PeopleMapper peopleMapper;

    @Autowired
    private HouseholdResidenceMapper householdResidenceMapper;

    @Autowired
    private HouseholdRegisterHistoryMapper householdRegisterHistoryMapper;


    /**
     * 查询人员管理
     * 
     * @param id 人员管理主键
     * @return 人员管理
     */
    @Override
    public People selectPeopleById(Long id)
    {
        return peopleMapper.selectPeopleById(id);
    }

    /**
     * 查询人员管理列表
     * 
     * @param people 人员管理
     * @return 人员管理
     */
    @Override
    public List<People> selectPeopleList(People people)
    {
        return peopleMapper.selectPeopleList(people);
    }

    /**
     * 新增人员管理
     * 
     * @param people 人员管理
     * @return 结果
     */
    @Override
    public int insertPeople(People people)
    {
        people.setCreateBy(SecurityUtils.getUsername());
        people.setCreateTime(DateUtils.getNowDate());
        int row = peopleMapper.insertPeople(people);
        householdRegisterHistoryMapper.insertHouseholdResidenceHistory(new HouseholdRegisterHistory("新登记人员",people.getHouseholdRegisterId(),people.getId(),SecurityUtils.getUsername()));
        if(people.getHouseholdRegisterId()!=0){
            householdResidenceMapper.updatePeopleCount(people.getHouseholdRegisterId());
        }
        return row;
    }

    /**
     * 修改人员管理
     * 
     * @param people 人员管理
     * @return 结果
     */
    @Override
    public int updatePeople(People people)
    {
        people.setUpdateBy(SecurityUtils.getUsername());
        people.setUpdateTime(DateUtils.getNowDate());
        householdRegisterHistoryMapper.insertHouseholdResidenceHistory(new HouseholdRegisterHistory("修改人员信息",people.getHouseholdRegisterId(),people.getId(),SecurityUtils.getUsername()));
        return peopleMapper.updatePeople(people);
    }

    /**
     * 批量删除人员管理
     * 
     * @param ids 需要删除的人员管理主键
     * @return 结果
     */
    @Override
    public int deletePeopleByIds(Long[] ids)
    {
        return peopleMapper.deletePeopleByIds(ids);
    }

    /**
     * 删除人员管理信息
     * 
     * @param id 人员管理主键
     * @return 结果
     */
    @Override
    public int deletePeopleById(Long id)
    {
        return peopleMapper.deletePeopleById(id);
    }

    /**
     * 迁出
     * @param people
     * @return
     */
    @Override
    public int out(People people) {
        people.setStatus("迁出");
        people.setOutTime(DateUtils.getNowDate());
        householdRegisterHistoryMapper.insertHouseholdResidenceHistory(new HouseholdRegisterHistory("人员迁出:"+people.getOutReason(),people.getHouseholdRegisterId(),people.getId(),SecurityUtils.getUsername()));
        return peopleMapper.updatePeople(people);
    }

    /**
     * 查询人口总数
     * @return
     */
    @Override
    public String selectCountPeople(){
        return peopleMapper.selectCountPeople();
    }

    /**
     * 查询最近七天迁入迁出数据
     * @return
     */
    @Override
    public List<CategroyCount> selectCategroyCount(){
        List<CategroyCount> categroyCounts=new ArrayList<>();
        categroyCounts.add(new CategroyCount(peopleMapper.selectCategroyCount("迁入"),"迁入"));
        categroyCounts.add(new CategroyCount(peopleMapper.selectCategroyCount("暂住"),"暂住"));
        categroyCounts.add(new CategroyCount(peopleMapper.selectCategroyCount("迁出"),"迁出"));
        return categroyCounts;
    }

    /**
     * 查询每个级别文化程度数量
     * @return
     */
    @Override
    public Map selectEducationLevelCount(){
        Map<String,Object> map=new HashMap<>();
        List<String> label = new ArrayList<>();
        List<EducationLevel> educationLevels = peopleMapper.selectEducationLevelCount();
        for (EducationLevel educationLevel : educationLevels) {
            if(!map.containsKey(educationLevel.getName())){
                map.put(educationLevel.getName(),1);
                label.add(educationLevel.getName());
            }
        }
        map.put("data",educationLevels);
        map.put("label",label);
        return map;
    }

    /**
     * 获取最近7天操作人数
     * @return
     */
    @Override
    public Map selectCountByDate(){
        Map<String,Object> map = new HashMap<>();
        map.put("people",peopleMapper.selectPeopleCountByDate());
        map.put("residence",peopleMapper.selectResidenceCountByDate());
        return map;
    }

    /**
     * 查询每个级别文化程度数量
     * @return
     */
    @Override
    public Map selectSexCount(){
        Map<String,Object> map=new HashMap<>();
        List<String> label = new ArrayList<>();
        List<EducationLevel> educationLevels = peopleMapper.selectSexCount();
        for (EducationLevel educationLevel : educationLevels) {
            if(!map.containsKey(educationLevel.getName())){
                map.put(educationLevel.getName(),1);
                label.add(educationLevel.getName());
            }
        }
        map.put("data",educationLevels);
        map.put("label",label);
        return map;
    }


}
